package cn.pink.core;

import cn.pink.core.support.serializer.SerializerUtil;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.eventbus.MessageCodec;

/**
 * 分布式消息解码器
 * @Author: pink
 * @Date: 2021/12/10 17:44
 */
public class CallMessageCodec implements MessageCodec<Call, Call> {
    @Override
    public void encodeToWire(Buffer buffer, Call call) {
        byte[] b = SerializerUtil.serialize(call);

        buffer.appendInt(b.length);
        buffer.appendBytes(b);
    }

    @Override
    public Call decodeFromWire(int pos, Buffer buffer) {
        int length = buffer.getInt(pos);
        return (Call) SerializerUtil.deserialize(buffer.getBytes(pos + 4, pos + 4 + length));
    }

    @Override
    public Call transform(Call call) {
        return call;
    }

    @Override
    public String name() {
        return "call";
    }

    @Override
    public byte systemCodecID() {
        return -1;
    }
}
